<?php

namespace app\award;

use app\models\order\Order;
use app\models\order\OrderItem;
use app\models\product\Product;
use app\models\product\ProductAreaList;
use app\models\Queue;
use app\models\SetProductCommission;
use app\models\SetUserUp;
use app\models\user\User;
use app\models\user\UserAccount;
use app\models\user\UserAddress;
use app\models\user\UserCommission;
use app\models\user\UserLevel;
use app\models\UserBalance;
use app\models\UserStore;
use app\models\UserStoreLog;
use app\services\Chain;
use yii\helpers\ArrayHelper;

class Award
{
    protected $user;
    protected $order;
    protected $set;

    protected $items;

    // 总金额
    protected $amount;

    // 复购商品单价
    protected $price;

    // 复购数量
    protected $box;

    protected $is_bao = false;
    protected $is_repeat = false;

    // 是否为重算订单
    protected $is_reset = false;


    public function __construct(Order $order, $is_reset = false)
    {
        $this->order = $order;
        $this->user = User::findOne(['id' => $order->user_id]);
        $this->set = SetUserUp::getLastOne();

        $this->is_reset = $is_reset;

        $this->items = OrderItem::find()
            ->where(['order_id' => $this->order->id])
            ->all();

        $first_order_item = OrderItem::find()
            ->where(['order_id' => $this->order->id])
            ->orderBy(['id' => SORT_ASC])
            ->limit(1)
            ->one();
        $product_id = $first_order_item->product_id;

        // 复购商品价格
        $this->price = Product::find()
            ->select(['price'])
            ->where(['id' => $product_id])
            ->scalar();

        $this->amount = $this->order->total_pay - $this->order->total_freight;

        // 报单区
        $this->is_bao = ProductAreaList::checkInArea($product_id, Product::$areaMap['REPORT']);
        if ($this->is_bao) {
            $this->bao();
        }

        // 复购区
        $this->is_repeat = ProductAreaList::checkInArea($product_id, Product::$areaMap['REPEAT']);
        if ($this->is_repeat) {
            $this->order->is_repeat = 1;
            $this->order->save();
            $this->repeat();
        }


    }

    // 报单
    public function bao()
    {
        $r = C::getReferrer($this->user->id);
        if (!$r || $r->level < UserLevel::$keys['vip1']) {
            return;
        }

        $name1 = '推荐奖';
        $type1 = 11;
        $name2 = '间荐奖';
        $type2 = 12;

        // 董事
        if ($this->amount == $this->set->price5) {
            if ($r->v5 >= UserLevel::$keys['vip10']) {
                $amount = $this->set->c5_1 + $this->set->c5_2;
                $this->saveCommission($r->id, $type1, $name1);
                $this->equal($r->id, 'v5', $amount);
            } else if ($r->v5 == UserLevel::$keys['vip9']) {
                $this->saveCommission($r->id, $this->set->c5_1, $type1, $name1);
                $r2 = $this->getIndirect($r->id, 'v5', $this->set->c5_1);
                if ($r2) {
                    $this->saveCommission($r2->id, $this->set->c5_2, $type2, $name2);
                    $this->equal($r2->id, 'v5', $this->set->c5_2);
                }
            } else {
                // 低推高减半
                $this->saveCommission($r->id, $this->set->c5_1 / 2, $type1, $name1);
            }
        }

        // CEO
        if ($this->amount == $this->set->price4) {
            if ($r->v4 >= UserLevel::$keys['vip8']) {
                $amount = $this->set->c4_1 + $this->set->c4_2;
                $this->saveCommission($r->id, $amount, $type1, $name1);
                $this->equal($r->id, 'v4', $amount);
            } else if ($r->v4 == UserLevel::$keys['vip7']) {
                $this->saveCommission($r->id, $this->set->c4_1, $type1, $name1);
                $r2 = $this->getIndirect($r->id, 'v4', $this->set->c4_1);
                if ($r2) {
                    $this->saveCommission($r2->id, $this->set->c4_2, $type2, $name2);
                    $this->equal($r2->id, 'v4', $this->set->c4_2);
                }
            } else {
                // 低推高减半
                $this->saveCommission($r->id, $this->set->c4_1 / 2, $type1, $name1);
            }
        }

        // 总裁
        if ($this->amount == $this->set->price3) {
            if ($r->v3 >= UserLevel::$keys['vip6']) {
                $amount = $this->set->c3_1 + $this->set->c3_2;
                $this->saveCommission($r->id, $amount, $type1, $name1);
                $this->equal($r->id, 'v3', $amount);
            } else if ($r->v3 == UserLevel::$keys['vip5']) {
                $this->saveCommission($r->id, $this->set->c3_1, $type1, $name1);
                $r2 = $this->getIndirect($r->id, 'v3', $this->set->c3_1);
                if ($r2) {
                    $this->saveCommission($r2->id, $this->set->c3_2, $type2, $name2);
                    $this->equal($r2->id, 'v3', $this->set->c3_2);
                }
            } else {
                // 低推高减半
                $this->saveCommission($r->id, $this->set->c3_1 / 2, $type1, $name1);
            }
        }

        // 总代
        if ($this->amount == $this->set->price2) {
            if ($r->v2 >= UserLevel::$keys['vip4']) {
                $amount = $this->set->c2_1 + $this->set->c2_2;
                $this->saveCommission($r->id, $amount, $type1, $name1);
                $this->equal($r->id, 'v2', $amount);
            } else if ($r->v2 == UserLevel::$keys['vip3']) {
                $this->saveCommission($r->id, $this->set->c2_1, $type1, $name1);
                $r2 = $this->getIndirect($r->id, 'v2', $this->set->c2_1);
                if ($r2) {
                    $this->saveCommission($r2->id, $this->set->c2_2, $type2, $name2);
                    $this->equal($r2->id, 'v2', $this->set->c2_2);
                }
            } else {
                // 低推高减半
                $this->saveCommission($r->id, $this->set->c2_1 / 2, $type1, $name1);
            }
        }

        // 会员
        if ($this->amount == $this->set->price1) {
            if ($r->v1 >= UserLevel::$keys['vip2']) {
                $amount = $this->set->c1_1 + $this->set->c1_2;
                $this->saveCommission($r->id, $amount, $type1, $name1);
                $this->equal($r->id, 'v1', $amount);
            } else if ($r->v1 == UserLevel::$keys['vip1']) {
                $this->saveCommission($r->id, $this->set->c1_1, $type1, $name1);
                $r2 = $this->getIndirect($r->id, 'v1', $this->set->c1_1);
                if ($r2) {
                    $this->saveCommission($r2->id, $this->set->c1_2, $type2, $name2);
                    $this->equal($r2->id, 'v1', $this->set->c1_2);
                }
            } else {
                // 低推高减半
                $this->saveCommission($r->id, $this->set->c1_1 / 2, $type1, $name1);
            }
        }
    }

    // 平级奖
    public function equal($user_id, $v, $amount)
    {
        $name = '平级奖';
        $type = 20;
        $commission = 0;
        if ($v == 'v1') {
            $commission = $this->set->e1;
        }
        if ($v == 'v2') {
            $commission = $this->set->e2;
        }
        if ($v == 'v3') {
            $commission = $this->set->e3;
        }
        if ($v == 'v4') {
            $commission = $this->set->e4;
        }
        if ($v == 'v5') {
            $commission = $this->set->e5;
        }

        $commission = $commission * $amount;

        $chain = C::getReferrerChainLevel($user_id, $v);
        foreach ($chain as $user) {
            $commission /= 100;
            // 小于1块, 则不返了
            if ($commission < 1) {
                break;
            }
            $this->saveCommission($user->id, $commission, $type, $name);
        }
    }

    // 复购
    public function repeat()
    {
        // 上次有库存, 扣上级的, 否则从平台走
        $r = C::getReferrer($this->user->id);
        $box = 0;
        foreach ($this->items as $item) {
            $box += $item['quantity'];
        }
        $this->box = $box;
        // var_dump($box);exit;

        if ($r) {
            $store = UserStore::find()
                ->where(['user_id' => $r->id])
                ->one();
            if ($store && intval($store->num) >= $box) {
                $diff = $this->diff($r);

                // 减云仓
                if (!$this->is_reset) {
                    UserStoreLog::addLog($r->id, $store->num, -$box, $store->num - $box,
                        '出货', 2, $this->order->id);
                    $store->num -= $box;
                    $store->save();
                }
                $this->saveCommission($r->id, $this->amount - $diff, 31, '出货');

                // 非平台出货
                $this->order->is_platform = 0;
                $this->order->save();

            } else {
                $this->diff();
            }
        } else {
            $this->diff();
        }
    }

    // 复购差价
    // 指定的用户, 不拿差价, 直接拿出货价
    public function diff(User $the_user = null)
    {
        $sum = 0;

        $name = '差价';
        $type = 30;

        $h_1 = false;
        $h_2 = false;
        $h_3 = false;
        $h_4 = false;
        $h_5 = false;
        $h_6 = false;
        $h_7 = false;
        $h_8 = false;
        $h_9 = false;
        $h_10 = false;

        $h_11 = false;
        $h_12 = false;

        $h_21 = false;
        $h_22 = false;
        $h_23 = false;
        $h_24 = false;
        $h_25 = false;
        $h_26 = false;
        $h_27 = false;

        $d_1 = 0;
        $d_2 = 0;
        $d_3 = 0;
        $d_4 = 0;
        $d_5 = 0;
        $d_6 = 0;
        $d_7 = 0;
        $d_8 = 0;
        $d_9 = 0;
        $d_10 = 0;

        $d_11 = 0;
        $d_12 = 0;

        $d_21 = 0;
        $d_22 = 0;
        $d_23 = 0;
        $d_24 = 0;
        $d_25 = 0;
        $d_26 = 0;
        $d_27 = 0;

        $chain = Chain::getReferrerChain($this->user->id, [
            UserLevel::$keys['vip1'],
            UserLevel::$keys['vip2'],
            UserLevel::$keys['vip3'],
            UserLevel::$keys['vip4'],
            UserLevel::$keys['vip5'],
            UserLevel::$keys['vip6'],
            UserLevel::$keys['vip7'],
            UserLevel::$keys['vip8'],
            UserLevel::$keys['vip9'],
            UserLevel::$keys['vip10'],

            // 加上两个新等级
            UserLevel::$keys['vip11'],
            UserLevel::$keys['vip12'],

            // 这里加上了运营中心, 区代,
            UserLevel::$keys['svip1'],
            UserLevel::$keys['svip2'],
            UserLevel::$keys['svip3'],

            UserLevel::$keys['svip4'],
            UserLevel::$keys['svip5'],
            UserLevel::$keys['svip6'],
            UserLevel::$keys['svip7'],
        ], true);

        foreach ($chain as $user) {
            if ($user->level <= $this->user->level) {
                continue;
            }

            if ((!$h_1 && !$h_2 && !$h_3 && !$h_4 && !$h_5 && !$h_6 && !$h_7 && !$h_8 && !$h_9 && !$h_10
                    && !$h_11 && !$h_12 && !$h_21 && !$h_22 && !$h_23 && !$h_24 && !$h_25 && !$h_26 && !$h_27) && $user->level == UserLevel::$keys['vip1']) {
                // $d_1 = $this->_diff($this->set->re1);
                $d_1 = $this->_diff('r1');
                $h_1 = true;
                if (!$the_user || ($the_user && $the_user->id != $user->id)) {
                    $this->saveCommission($user->id, $d_1, $type, $name);
                    $sum += $d_1;
                }
            }
            if ((!$h_2 && !$h_3 && !$h_4 && !$h_5 && !$h_6 && !$h_7 && !$h_8 && !$h_9 && !$h_10
                    && !$h_11 && !$h_12 && !$h_21 && !$h_22 && !$h_23 && !$h_24 && !$h_25 && !$h_26 && !$h_27) && $user->level == UserLevel::$keys['vip2']) {
                // $d_2 = $this->_diff($this->set->re2);
                $d_2 = $this->_diff('r2');
                $d_2 -= $d_1;
                $h_2 = true;
                if (!$the_user || ($the_user && $the_user->id != $user->id)) {
                    $this->saveCommission($user->id, $d_2, $type, $name);
                    $sum += $d_2;
                }
            }
            if ((!$h_3 && !$h_4 && !$h_5 && !$h_6 && !$h_7 && !$h_8 && !$h_9 && !$h_10
                    && !$h_11 && !$h_12 && !$h_21 && !$h_22 && !$h_23 && !$h_24 && !$h_25 && !$h_26 && !$h_27) && $user->level == UserLevel::$keys['vip3']) {
                // $d_3 = $this->_diff($this->set->re3);
                $d_3 = $this->_diff('r3');
                $d_3 -= $d_2 + $d_1;
                $h_3 = true;
                if (!$the_user || ($the_user && $the_user->id != $user->id)) {
                    $this->saveCommission($user->id, $d_3, $type, $name);
                    $sum += $d_3;
                }
            }
            if ((!$h_4 && !$h_5 && !$h_6 && !$h_7 && !$h_8 && !$h_9 && !$h_10
                    && !$h_11 && !$h_12 && !$h_21 && !$h_22 && !$h_23 && !$h_24 && !$h_25 && !$h_26 && !$h_27) && $user->level == UserLevel::$keys['vip4']) {
                // $d_4 = $this->_diff($this->set->re4);
                $d_4 = $this->_diff('r4');
                $d_4 -= $d_2 + $d_3 + $d_1;
                $h_4 = true;
                if (!$the_user || ($the_user && $the_user->id != $user->id)) {
                    $this->saveCommission($user->id, $d_4, $type, $name);
                    $sum += $d_4;
                }
            }
            if ((!$h_5 && !$h_6 && !$h_7 && !$h_8 && !$h_9 && !$h_10
                    && !$h_11 && !$h_12 && !$h_21 && !$h_22 && !$h_23 && !$h_24 && !$h_25 && !$h_26 && !$h_27) && $user->level == UserLevel::$keys['vip5']) {
                // $d_5 = $this->_diff($this->set->re5);
                $d_5 = $this->_diff('r5');
                $d_5 -= $d_4 + $d_3 + $d_2 + $d_1;
                $h_5 = true;
                if (!$the_user || ($the_user && $the_user->id != $user->id)) {
                    $this->saveCommission($user->id, $d_5, $type, $name);
                    $sum += $d_5;
                }
            }
            if ((!$h_6 && !$h_7 && !$h_8 && !$h_9 && !$h_10
                    && !$h_11 && !$h_12 && !$h_21 && !$h_22 && !$h_23 && !$h_24 && !$h_25 && !$h_26 && !$h_27) && $user->level == UserLevel::$keys['vip6']) {
                // $d_6 = $this->_diff($this->set->re6);
                $d_6 = $this->_diff('r6');
                $d_6 -= $d_5 + $d_4 + $d_3 + $d_2 + $d_1;
                $h_6 = true;
                if (!$the_user || ($the_user && $the_user->id != $user->id)) {
                    $this->saveCommission($user->id, $d_6, $type, $name);
                    $sum += $d_6;
                }
            }
            if ((!$h_7 && !$h_8 && !$h_9 && !$h_10
                    && !$h_11 && !$h_12 && !$h_21 && !$h_22 && !$h_23 && !$h_24 && !$h_25 && !$h_26 && !$h_27) && $user->level == UserLevel::$keys['vip7']) {
                // $d_7 = $this->_diff($this->set->re7);
                $d_7 = $this->_diff('r7');
                $d_7 -= $d_6 + $d_5 + $d_4 + $d_3 + $d_2 + $d_1;
                $h_7 = true;
                if (!$the_user || ($the_user && $the_user->id != $user->id)) {
                    $this->saveCommission($user->id, $d_7, $type, $name);
                    $sum += $d_7;
                }
            }
            if ((!$h_8 && !$h_9 && !$h_10
                    && !$h_11 && !$h_12 && !$h_21 && !$h_22 && !$h_23 && !$h_24 && !$h_25 && !$h_26 && !$h_27) && $user->level == UserLevel::$keys['vip8']) {
                // $d_8 = $this->_diff($this->set->re8);
                $d_8 = $this->_diff('r8');
                $d_8 -= $d_7 + $d_6 + $d_5 + $d_4 + $d_3 + $d_2 + $d_1;
                $h_8 = true;
                if (!$the_user || ($the_user && $the_user->id != $user->id)) {
                    $this->saveCommission($user->id, $d_8, $type, $name);
                    $sum += $d_8;
                }
            }
            if ((!$h_9 && !$h_10
                    && !$h_11 && !$h_12 && !$h_21 && !$h_22 && !$h_23 && !$h_24 && !$h_25 && !$h_26 && !$h_27) && $user->level == UserLevel::$keys['vip9']) {
                // $d_9 = $this->_diff($this->set->re9);
                $d_9 = $this->_diff('r9');
                $d_9 -= $d_8 + $d_7 + $d_6 + $d_5 + $d_4 + $d_3 + $d_2 + $d_1;
                $h_9 = true;
                if (!$the_user || ($the_user && $the_user->id != $user->id)) {
                    $this->saveCommission($user->id, $d_9, $type, $name);
                    $sum += $d_9;
                }
            }
            if ((!$h_10
                    && !$h_11 && !$h_12 && !$h_21 && !$h_22 && !$h_23 && !$h_24 && !$h_25 && !$h_26 && !$h_27) && $user->level == UserLevel::$keys['vip10']) {
                // $d_10 = $this->_diff($this->set->re10);
                $d_10 = $this->_diff('r10');
                $d_10 -= $d_9 + $d_8 + $d_7 + $d_6 + $d_5 + $d_4 + $d_3 + $d_2 + $d_1;
                $h_10 = true;
                if (!$the_user || ($the_user && $the_user->id != $user->id)) {
                    $this->saveCommission($user->id, $d_10, $type, $name);
                    $sum += $d_10;
                }
                // break;
            }

            if ((!$h_11 && !$h_12 && !$h_21 && !$h_22 && !$h_23 && !$h_24 && !$h_25 && !$h_26 && !$h_27) && $user->level == UserLevel::$keys['vip11']) {
                $d_11 = $this->_diff('r11');
                $d_11 -= $d_10 + $d_9 + $d_8 + $d_7 + $d_6 + $d_5 + $d_4 + $d_3 + $d_2 + $d_1;
                $h_11 = true;
                if (!$the_user || ($the_user && $the_user->id != $user->id)) {
                    $this->saveCommission($user->id, $d_11, $type, $name);
                    $sum += $d_11;
                }
            }
            if ((!$h_12 && !$h_21 && !$h_22 && !$h_23 && !$h_24 && !$h_25 && !$h_26 && !$h_27) && $user->level == UserLevel::$keys['vip12']) {
                $d_12 = $this->_diff('r12');
                $d_12 -= $d_11 + $d_10 + $d_9 + $d_8 + $d_7 + $d_6 + $d_5 + $d_4 + $d_3 + $d_2 + $d_1;
                $h_12 = true;
                if (!$the_user || ($the_user && $the_user->id != $user->id)) {
                    $this->saveCommission($user->id, $d_12, $type, $name);
                    $sum += $d_12;
                }
            }

            if ((!$h_21 && !$h_22 && !$h_23 && !$h_24 && !$h_25 && !$h_26 && !$h_27) && $user->level == UserLevel::$keys['svip1']) {
                $d_21 = $this->_diff('r101');
                $d_21 -= $d_12 + $d_11 + $d_10 + $d_9 + $d_8 + $d_7 + $d_6 + $d_5 + $d_4 + $d_3 + $d_2 + $d_1;
                $h_21 = true;
                if (!$the_user || ($the_user && $the_user->id != $user->id)) {
                    $this->saveCommission($user->id, $d_21, $type, $name);
                    $sum += $d_21;
                }
            }
            if ((!$h_22 && !$h_23 && !$h_24 && !$h_25 && !$h_26 && !$h_27) && $user->level == UserLevel::$keys['svip2']) {
                $d_22 = $this->_diff('r102');
                $d_22 -= $d_21 + $d_12 + $d_11 + $d_10 + $d_9 + $d_8 + $d_7 + $d_6 + $d_5 + $d_4 + $d_3 + $d_2 + $d_1;
                $h_22 = true;
                if (!$the_user || ($the_user && $the_user->id != $user->id)) {
                    $this->saveCommission($user->id, $d_22, $type, $name);
                    $sum += $d_22;
                }
            }
            if ((!$h_23 && !$h_24 && !$h_25 && !$h_26 && !$h_27) && $user->level == UserLevel::$keys['svip3']) {
                $d_23 = $this->_diff('r103');
                $d_23 -= $d_22 + $d_21 + $d_12 + $d_11 + $d_10 + $d_9 + $d_8 + $d_7 + $d_6 + $d_5 + $d_4 + $d_3 + $d_2 + $d_1;
                $h_23 = true;
                if (!$the_user || ($the_user && $the_user->id != $user->id)) {
                    $this->saveCommission($user->id, $d_23, $type, $name);
                    $sum += $d_23;
                }
            }

            if ((!$h_24 && !$h_25 && !$h_26 && !$h_27) && $user->level == UserLevel::$keys['svip4']) {
                $d_24 = $this->_diff('r201');
                $d_24 -= $d_23 + $d_22 + $d_21 + $d_12 + $d_11 + $d_10 + $d_9 + $d_8 + $d_7 + $d_6 + $d_5 + $d_4 + $d_3 + $d_2 + $d_1;
                $h_24 = true;
                if (!$the_user || ($the_user && $the_user->id != $user->id)) {
                    $this->saveCommission($user->id, $d_24, $type, $name);
                    $sum += $d_24;
                }
            }
            if ((!$h_25 && !$h_26 && !$h_27) && $user->level == UserLevel::$keys['svip5']) {
                $d_25 = $this->_diff('r202');
                $d_25 -= $d_24 + $d_23 + $d_22 + $d_21 + $d_12 + $d_11 + $d_10 + $d_9 + $d_8 + $d_7 + $d_6 + $d_5 + $d_4 + $d_3 + $d_2 + $d_1;
                $h_25 = true;
                if (!$the_user || ($the_user && $the_user->id != $user->id)) {
                    $this->saveCommission($user->id, $d_25, $type, $name);
                    $sum += $d_25;
                }
            }
            if ((!$h_26 && !$h_27) && $user->level == UserLevel::$keys['svip6']) {
                $d_26 = $this->_diff('r203');
                $d_26 -= $d_25 + $d_24 + $d_23 + $d_22 + $d_21 + $d_12 + $d_11 + $d_10 + $d_9 + $d_8 + $d_7 + $d_6 + $d_5 + $d_4 + $d_3 + $d_2 + $d_1;
                $h_26 = true;
                if (!$the_user || ($the_user && $the_user->id != $user->id)) {
                    $this->saveCommission($user->id, $d_26, $type, $name);
                    $sum += $d_26;
                }
            }
            if ((!$h_27) && $user->level == UserLevel::$keys['svip7']) {
                $d_27 = $this->_diff('r204');
                $d_27 -= $d_26 + $d_25 + $d_24 + $d_23 + $d_22 + $d_21 + $d_12 + $d_11 + $d_10 + $d_9 + $d_8 + $d_7 + $d_6 + $d_5 + $d_4 + $d_3 + $d_2 + $d_1;
                $h_27 = true;
                if (!$the_user || ($the_user && $the_user->id != $user->id)) {
                    $this->saveCommission($user->id, $d_27, $type, $name);
                    $sum += $d_27;
                }
                break;
            }
        }

        return $sum;
    }

    // 计算差价(打折)
    // protected function _diff($set)
    // {
    //     // 支付金额 - 折扣价 * 数量
    //     $amount = $this->amount - ($set / 10 * $this->price) * $this->box;
    //
    //     return $amount;
    // }

    // 这个是最新的差价
    protected function _diff($diff_key)
    {
        $order_items = $this->items;

        $sum = 0;
        foreach ($order_items as $item) {
            $product = SetProductCommission::findOne(['product_id' => $item->product_id])->toArray();

            // var_dump($product);exit;

            $sum += ($item['price'] - $product[$diff_key]) * $item['quantity'];
        }

        // 用订单价 - 复购价 = 差价
        return $sum;

    }

    // 2 + 1 上级
    private function getIndirect($user_id, $v)
    {
        $the_referrer = C::getReferrerLevel($user_id, $v);
        if (!$the_referrer) {
            return null;
        }

        $children = C::getChildrenSort($the_referrer->id, $v);
        if (count($children) > 2) {
            $has = [$children[0]['id'], $children[1]['id']];
            if (!in_array($user_id, $has)) {
            // if (true || !in_array($user_id, $has)) {
                return $the_referrer;
            } else {
                return $this->getIndirect($the_referrer->id, $v);
            }
        } else {
            return $this->getIndirect($the_referrer->id, $v);
        }
    }

    // 保存佣金 (await balance)
    protected function saveCommission($user_id, $balance, $type, $name, $status = 0)
    {
        if ($balance >= 0.01) {
            $model = new UserCommission();
            $model->user_id = $user_id;
            $model->fans_id = $this->order->user_id;
            $model->order_id = $this->order->id;
            $model->balance = $balance;
            $model->status = $status;
            $model->award_name = $name;
            $model->award_type = $type;
            $model->save();
            $account = UserAccount::findOne(['user_id' => $user_id]);

            if ($account) {
                if ($status == 1) {
                    $account->balance += $balance;
                } else {
                    $account->await_balance += $balance;
                }

              $account->save();
            }

            // balance logs
            if ($status == 1) {
                UserBalance::addLog($model->user_id,
                    $account->balance, $model->balance, $account->balance + $model->balance, '返佣', $this->order->id);
            }

            return true;
        }
    }

}